﻿namespace D.UtilSqlsugarOrm
{
    /// <summary>
    /// 业务逻辑接口实现层
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseService<T> : IBaseService<T> where T : class, new()
    {
        /// <summary>
        /// 通过在子类的构造函数中注入，这里是基类，不用构造函数，用于base方法
        /// </summary>
        public IBaseRepository<T>? _baseDal;
         
        #region 新增

        /// <summary>
        /// 单个新增
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T t)
        {
            return await _baseDal.AddAsync(t);
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<int> AddListAsync(List<T> ts)
        {
            return await _baseDal.AddListAsync(ts);
        }

        /// <summary>
        /// 新增或更新
        /// </summary>
        /// <param name="ts">数据集合</param>
        /// <returns>返回新增数量,返回更新数量</returns>
        public async Task<Tuple<int, int>> InsertOrUpdateAsync(List<T> ts)
        {
            return await _baseDal.InsertOrUpdateAsync(ts);
        }
        #endregion

        #region 删除

        /// <summary>
        /// 普通删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(T t)
        {
            return await _baseDal.DeleteAsync(t);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(List<T> ts)
        {
            return await _baseDal.DeleteAsync(ts);
        }

        /// <summary>
        /// 执行SQL删除
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(string sql)
        {
            return await _baseDal.DeleteAsync(sql);
        }

        /// <summary>
        /// 根据条件删除
        /// </summary>
        /// <param name="where">表达式条件</param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(System.Linq.Expressions.Expression<Func<T, bool>> where)
        {
            return await _baseDal.DeleteAsync(where);
        }

        #endregion

        #region 修改

        /// <summary>
        /// 单个修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T t)
        {
            return await _baseDal.UpdateAsync(t);
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(List<T> ts)
        {
            return await _baseDal.UpdateAsync(ts);
        }

        /// <summary>
        /// 根据条件修改
        /// </summary>
        /// <param name="columns">修改列</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(System.Linq.Expressions.Expression<Func<T, T>> columns, System.Linq.Expressions.Expression<Func<T, bool>> where)
        {
            return await _baseDal.UpdateAsync(columns, where);
        }

        #endregion

        #region 查询

        /// <summary>
        /// 单个查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<T> GetModelAsync(System.Linq.Expressions.Expression<Func<T, bool>> where)
        {
            return await _baseDal.GetModelAsync(where);
        }


        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<List<T>> GetListAsync()
        {
            return await _baseDal.GetListAsync();
        }

        /// <summary>
        /// 获取所有列表
        /// </summary>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<T>> GetListAsync(System.Linq.Expressions.Expression<Func<T, bool>> where, System.Linq.Expressions.Expression<Func<T, object>> order = null)
        {
            return await _baseDal.GetListAsync(where, order);
        }

        /// <summary>
        /// 获取多少个列表
        /// </summary>
        /// <param name="where"></param>
        /// <param name="tack"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<T>> GetListAsync(System.Linq.Expressions.Expression<Func<T, bool>> where, int tack, System.Linq.Expressions.Expression<Func<T, object>> order = null)
        {
            return await _baseDal.GetListAsync(where, tack, order);
        }

        /// <summary>
        /// 排序获取单个对象
        /// </summary>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<T> GetMaxModelAsync(System.Linq.Expressions.Expression<Func<T, bool>> where, System.Linq.Expressions.Expression<Func<T, object>> order)
        {
            return await _baseDal.GetOrderByModelAsync(where, order);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageSize">每页显示多少条</param>
        /// <param name="current">当前第几页</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PageModel<T>> GetPagesAsync(int pageSize, int current)
        {
            return await _baseDal.GetPagesAsync(pageSize, current);
        }

        /// <summary>
        /// 分页查询 加入条件
        /// </summary>
        /// <param name="pageSize">每页显示多少条</param>
        /// <param name="current">当前第几页</param>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public async Task<PageModel<T>> GetPagesAsync(int pageSize, int current, System.Linq.Expressions.Expression<Func<T, bool>> where, System.Linq.Expressions.Expression<Func<T, object>> order = null)
        {
            return await _baseDal.GetPagesAsync(pageSize, current, where, order);
        }

        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<bool> ExistsAsync(System.Linq.Expressions.Expression<Func<T, bool>> where)
        {
            return await _baseDal.ExistsAsync(where);
        }

        /// <summary>
        /// 获取平均值
        /// </summary>
        /// <param name="where"></param>
        /// <param name="sum"></param>
        /// <returns></returns>

        public async Task<decimal> GetSumAsync(System.Linq.Expressions.Expression<Func<T, bool>> where, System.Linq.Expressions.Expression<Func<T, decimal>> sum)
        {
            return await _baseDal.GetSumAsync(where, sum);
        }

        /// <summary>
        /// 获取总数量
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<long> GetTotalCountAsync(System.Linq.Expressions.Expression<Func<T, bool>> where)
        {
            return await _baseDal.GetTotalCountAsync(where);
        }
        #endregion
    }
}
